package net.keebook.webapp.bean;

import java.io.IOException;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import net.keebook.webapp.dao.KeeBookDao;
import net.keebook.webapp.dao.ShareRequestDao;
import net.keebook.webapp.entity.KeeBook;
import net.keebook.webapp.entity.KeeBookAccess;
import net.keebook.webapp.entity.ShareRequest;
import net.keebook.webapp.model.SessionModel;
import net.keebook.webapp.util.AccessRight;
import net.keebook.webapp.util.Messages;
import net.keebook.webapp.util.ShareStatus;


@ManagedBean
@RequestScoped
public class ListSharesBean {
	@Inject
	private ShareRequestDao shareRequestDao;
	@Inject
	private KeeBookDao keeBookDao;
	private SessionModel sessionModel;
	private HtmlDataTable dataTable;
	private List<ShareStatus> shares = null;
	
	@PostConstruct
	public void init() throws IOException {
		setSessionModel(SessionModel.getInstance(FacesContext.getCurrentInstance()));
		
		if (!getKeeBookDao().getKeeBookAccess(getSessionModel(), AccessRight.OWNER)) {
			Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
			FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
			return;
		}
		
		HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
		String exposableId = request.getParameter("delete");
		if (exposableId != null && !"".equals(exposableId)) {
			if (!getKeeBookDao().getKeeBookAccess(getSessionModel(), AccessRight.OWNER)) return;
			
			ShareRequest sr = getShareRequestDao().get(exposableId);
			if (sr != null) {
				getShareRequestDao().delete(sr);
				FacesContext.getCurrentInstance().getExternalContext().redirect("listshares.xhtml?msg="+Messages.ITEM_DELETED);
				return;
			}
			
			KeeBookAccess kba = getKeeBookDao().getAccessRight(exposableId);
			if (kba != null) {
				getKeeBookDao().removeAccessRight(exposableId);
				FacesContext.getCurrentInstance().getExternalContext().redirect("listshares.xhtml?msg="+Messages.ITEM_DELETED);
				return;
			}
			
			FacesContext.getCurrentInstance().getExternalContext().redirect("listshares.xhtml?msg="+Messages.ITEM_DELETE_FAILED);
		}
	}
	
	public void update() throws IOException {
		if (!getKeeBookDao().getKeeBookAccess(getSessionModel(), AccessRight.OWNER)) {
			Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
			FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
			return;
		}
		
		KeeBook keeBook = getKeeBookDao().get(getSessionModel().getKeeBookId());
		
		for (int i=0; i<getDataTable().getRowCount(); i++) {
			getDataTable().setRowIndex(i);
			ShareStatus shareStatus = (ShareStatus)getDataTable().getRowData();
			if (!shareStatus.getSelf()) {
				
				ShareRequest sr = getShareRequestDao().get(shareStatus.getExposableId());
				if (sr != null) {
					sr.setAccessRight(shareStatus.getAccessRight());
					getShareRequestDao().update(sr);
				}
				else {
					KeeBookAccess kba = getKeeBookDao().getAccessRight(shareStatus.getExposableId());
					if (kba != null) {
						getKeeBookDao().setAccessRight(kba.getUser(), keeBook, shareStatus.getAccessRight());
					}
				}
				
			}
		}
		
		FacesContext.getCurrentInstance().getExternalContext().redirect("listshares.xhtml?msg="+Messages.SETTINGS_APPLIED);
	}
	
	public List<ShareStatus> getShares() {
		if (shares == null) {
			shares = getShareRequestDao().getShareStatus(getSessionModel().getKeeBookId(), getSessionModel().getUserId());
		}
		return shares;
	}

	public ShareRequestDao getShareRequestDao() {
		return shareRequestDao;
	}

	public void setShareRequestDao(ShareRequestDao shareRequestDao) {
		this.shareRequestDao = shareRequestDao;
	}

	public KeeBookDao getKeeBookDao() {
		return keeBookDao;
	}

	public void setKeeBookDao(KeeBookDao keeBookDao) {
		this.keeBookDao = keeBookDao;
	}

	public SessionModel getSessionModel() {
		return sessionModel;
	}

	public void setSessionModel(SessionModel sessionModel) {
		this.sessionModel = sessionModel;
	}

	public HtmlDataTable getDataTable() {
		return dataTable;
	}

	public void setDataTable(HtmlDataTable dataTable) {
		this.dataTable = dataTable;
	}
}
